package ee.siimoliver.chat.GUI;

import java.awt.Image;
import java.awt.Window;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

import ee.siimoliver.chat.GUI.dialogs.PropertiesDialog;
import ee.siimoliver.chat.controllers.BusinessLogicController;
import ee.siimoliver.chat.controllers.ChatSessionController;
import ee.siimoliver.chat.controllers.DialogController;
import ee.siimoliver.chat.utils.ChatUtils;
import ee.siimoliver.chat.utils.Const;

public class ChatFrame extends JFrame {

	private static final long serialVersionUID = 1L;
	private TabbedChatPanel chatTabsPanel;
	private MenuPanel menuPanel;
	private ArrayList<Image> loadingImages;
	private PropertiesDialog propertiesDialog;
	private BusinessLogicController controller;
	
	public ChatFrame() {
		super(Const.LBL_CHAT_APPLICATION);
		ChatUtils.setPlatformSpecificLookAndFeel();
		initFields();
		ChatUtils.centerWindow(this);
	}
	
	public void run() {
		try {
			propertiesDialog.setVisible(true);
//			if (ApplicationProperties.getInstance().getProperty(Const.PROP_USERNAME).trim().isEmpty()) {
//				propertiesDialog.setVisible(true);
//			} else {
//				setVisible(true);
//			}
		} catch (Exception e) {
			System.err.println("Properties file corrupted!");
		}
	}
	
	private void initFields() {
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setSize(800, 600);
		loadingImages = loadImages();
		menuPanel = new MenuPanel(this);
		propertiesDialog = new PropertiesDialog(this);
		chatTabsPanel = new TabbedChatPanel();
		setJMenuBar(menuPanel);
//		chatTabsPanel.add("Oliver", new ChatContentPanel(this, null)); //TODO: remove dummytab
		setContentPane(chatTabsPanel);
	}

	public void showPropertiesDialog() {
		propertiesDialog.fillInputFields();
		propertiesDialog.setVisible(true);
		ChatUtils.centerWindowRelativeToParent(propertiesDialog, this);
		this.setEnabled(false);
	}

	public void searchNewChatPartner(String username, Window parent) {
		searchNewChatPartner(username, parent, null);
	}
	
	public void searchNewChatPartner(final String username, final Window parent, 
			final DialogController dialog) {
		if (controller != null) {
			LoadingFrame lf = new LoadingFrame(getLoadingImages(), this) {
				@Override
				protected void doInBackground() {
					try {
						String result = controller.searchChatmate(this, username);
						if (result.equals("")) {
							JOptionPane.showMessageDialog(parent, "Didn't find user!", 
									Const.TXT_ERROR, JOptionPane.ERROR_MESSAGE);
						} else {
							JOptionPane.showMessageDialog(parent, "Found user @" + result, 
									Const.TXT_ERROR, JOptionPane.INFORMATION_MESSAGE);
						}
						if (dialog != null) {
							dialog.updateContent();
						}
					} catch (Exception e) {
						JOptionPane.showMessageDialog(ChatFrame.this, e.getMessage(), Const.TXT_ERROR, JOptionPane.ERROR_MESSAGE);
					}
				}
			};
			lf.execute();
		}
	}
	
	public ChatSessionController addNewChatSession(ChatSessionController chatSession) {
		ChatContentPanel chatContentPanel = new ChatContentPanel(this, chatSession);
		chatSession.attachOutputController(chatContentPanel);
		chatTabsPanel.add(chatSession.getSessionName(), chatContentPanel, controller, chatSession);
		return chatSession;
	}
	
	public void attachBusinessLogicController(BusinessLogicController controller) {
		this.controller = controller;
	}
	
	public BusinessLogicController getBusinessLogicController() {
		return controller;
	}
	
	private ArrayList<Image> loadImages() {
		ArrayList<Image> output = new ArrayList<Image>();
		try {
			for (int i = 1; i < 13; i++) {
				output.add(ImageIO.read(new File(Const.LOADING_IMAGE + i + Const.IMAGE_EXT)));
			}
		} catch (IOException e) {
			
		}
		return output;
	}
	
	public ArrayList<Image> getLoadingImages() {
		return loadingImages;
	}
}
